import xmRequest from './request';
import cacheUtils from '@/utils/local';
import type { AxiosRequestConfig } from 'axios';

console.log('service index', process.env.NODE_ENV === 'development');

const request1 = new xmRequest({
  baseURL:
    process.env.NODE_ENV === 'development'
      ? '/coderwhy'
      : 'http://152.136.185.210:5000',
  // baseURL: 'http://123.207.32.32:8000/',
  timeout: 10000,
  interceptors: {
    requestInterceptor(config: AxiosRequestConfig<any>) {
      // console.log('请求成功拦截器');
      const token = cacheUtils.getCache('token');
      // config对象未定义的解决方案  axios版本问题
      // https://blog.csdn.net/qq_21473443/article/details/125064620
      if (token && config && config.headers) {
        config.headers.Authorization = `Bearer ${token}`;
      }
      return config;
    },
    requestInterceptorCatch(err) {
      // console.log('请求失败拦截器');
      return err;
    },
    reponseInterceptor(res) {
      // console.log('响应成功拦截器');
      return res;
    },
    reponseInterceptorCatch(err) {
      // console.log('响应失败拦截器');
      return err;
    },
  },
});

//#region
/* interface DateType {
  data: object;
  returnCode: string;
  success: boolean;
}

request1
  .request<DateType>({
    url: '/home/multidata',
    params: {
      name: 'xiaoming',
      age: 20,
    },
    // showLoading: false,
    interceptors: {
      requestInterceptor(config) {
        console.log('单独实例的请求拦截成功');
        return config;
      },
      reponseInterceptor(res) {
        console.log('单独实例的响应拦截成功');
        return res;
      },
    },
  })
  .then((res) => {
    console.log('service index.ts', res.data);

    // request1
    //   .request({
    //     url: '/get',
    //     params: {
    //       name: 'red',
    //       age: 50,
    //     },
    //     // showLoading: false,
    //   })
    //   .then((res) => {
    //     console.log(res);
    //   });
  }); */
//#endregion

export { request1 };
